<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-4-25
 * Time: 下午3:57
 * 云顶套票的子票信息
 * 当需要更新套票信息时 浏览器地址输入cs 重新获取套票信息 再复制进这个页面
 */
/* 一共读取两个静态文本
 * l是记录子票景区的id=>title 3.6天更新一次
 * t是最终返回的数组,0.25天更新一次
 * 如果l需要更新了 那么t也更新
 * 先判断l是否需要更新
 * */

$dir_1 = '/var/www/html/new/d/module/zax/yd_report/l.txt';
$dir = '/var/www/html/new/d/module/zax/yd_report/t.txt';
$a=filemtime($dir_1);
if($_SERVER['REQUEST_TIME']-$a<24400)
    goto one;
include_once '/var/www/html/new/com.inc.php';
$sql = "select id,title from uu_land where apply_did in(7132,7133,7134,7135) and p_type!='F' and status=1";
$GLOBALS['le']->query($sql);
$row = array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $land[$row['id']] = $row['title'];
}
file_get_contents($dir_1,json_encode($land));
goto two;//l.txt更新了的话 那么下一步就不判断 强制更新数据

one:
$a=filemtime($dir);
if($_SERVER['REQUEST_TIME']-$a>14400)//每4个小时刷新一次数据
    goto two;
$t = file_get_contents($dir);
return json_decode($t,true);

two:

/*
$land = array (
    5102 => '云顶景区直通车',
    5107 => '中国云顶蛋居酒店',
    5118 => '中国云顶山寨客栈',
    5123 => '中国云顶木屋别墅',
    5131 => '中国云顶帐篷酒店',
    5133 => '云顶景区好汉酒楼自助餐',
    5160 => '云顶景区好汉酒楼围餐',
    5714 => '【pzp测试】0113线路B',
    6983 => '【特价蛋居，每日限量】中国云顶蛋居酒店',
    7937 => '云顶景区景点门票',
    7950 => '云顶景区观光车票',
    7951 => '云顶景区索道票',
);
*/
include_once '/var/www/html/new/com.inc.php';
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$soap = new SoapClient(null,array(
        "location" =>"http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true")
);
//云顶所有套票
$sql = "select t.id as tid,t.pay,p.id,l.attribute from uu_land l left join uu_products p
        on p.contact_id=l.id left join uu_jq_ticket t on t.landid=l.id where
        l.p_type='F' and l.apply_did in(7132,7133,7134,7135)";
$GLOBALS['le']->query($sql);
$row = array();
$attribute = array();
$n = 0;
while($row=$GLOBALS['le']->fetch_assoc()){
    if(!$row['id']) continue;
    $attribute[] = array(
        json_decode($row['attribute'],true),
        $row['id'],
        $row['tid'],
        $row['pay']
    );
}
//    print_r($attribute);
//exit;
foreach($attribute as $k => $v){//对子票进行排序 依次是 景点 观光车 索道 酒店
    $lid = $XXX = array();//每次循环都要初始化
    $lids = '';
    foreach($v[0] as $ke => $va){
        $lid[] = $va['lid'];//记录原始顺序的key值
        $lids .= $va['lid'].',';
    }
    $lids = rtrim($lids,',');
    $sql = "select id,title,p_type from uu_land where id in($lids)";
    $GLOBALS['le']->query($sql);
    $row = array();
//    echo "<pre>";
    while($row=$GLOBALS['le']->fetch_assoc()){
//        print_r($row);
        $px = array_search($row['id'],$lid);//排序
//        echo $px;
        if($row['p_type']=='C'){
            $XXX[3]= $v[0][$px];
            continue;
        }
        if(strpos($row['title'],'观光车')){
            $XXX[1]= $v[0][$px];
            continue;
        }
        if(strpos($row['title'],'索道')){
            $XXX[2]= $v[0][$px];
            continue;
        }
        $XXX[0]= $v[0][$px];
    }
    ksort($XXX);
    $attribute[$k][0]=$XXX;
}
//print_r($attribute);
//echo "</pre>";
//exit;

$land = $land ? $land : json_decode(file_get_contents($dir_1),true);

foreach($attribute as $k => $v){
    $data = $data_2 = array();
    foreach($v[0] as $ke => $va){
        $data[] = array(
            'title'=>$land[$va['lid']],
            'price'=>price($va['pid'],$soap,0),
            'num'  =>$va['num']
        );
        $data_2[] = array(
            'title'=>$land[$va['lid']],
            'price'=>price($va['pid'],$soap,1),
            'num'  =>$va['num']
        );
    }
    $data_2['lsj'] = price($v[1],$soap,1);//零售价
    $ticket_data[$v[2]] = array(
        'pay'=>$v[3]==0?'local':'other',
        'data'=>$data,
        'data_2'=>$data_2);
}
//echo "<pre>";
//var_export($ticket_data);
file_get_contents($dir,json_encode($ticket_data));
return($ticket_data);
exit;
function price($pid,$soap,$type){//type 0 供应价 1 零售价
    $pid= $pid+0;
    if($type !=0 && $type !=1)
        return false;
    $today = date('Y-m-d');
    $fprice = $soap->get_Dynamic_Price_Merge($pid, $today, 1, '', '', $type);//当日价格
    if($fprice==-1)
        $fprice = $soap->get_Dynamic_Price_Merge($pid, '', 2, '', '', $type);//最低价
    return $fprice;
}